package com.mooc.ppjoke.exoplayer;

import android.graphics.Point;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: PageListplayerDetector$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/23 08:03
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class PageListPlayDetector {
    private List<IPlayTarget> mTargets=new ArrayList<>();
    private RecyclerView recyclerView;
    private IPlayTarget playerTarget;

    public void addTarget(IPlayTarget target){
        mTargets.add(target);
    }
    public void removeTarget(IPlayTarget target){
        mTargets.remove(target);
    }
    public PageListPlayDetector(LifecycleOwner owner, RecyclerView recyclerView){
        this.recyclerView = recyclerView;
        owner.getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull @NotNull LifecycleOwner source, @NonNull @NotNull Lifecycle.Event event) {
                if(event==Lifecycle.Event.ON_DESTROY){
                    recyclerView.getAdapter().unregisterAdapterDataObserver(mDataObserver);
                    owner.getLifecycle().removeObserver(this);
                }
            }
        });
        recyclerView.getAdapter().registerAdapterDataObserver(mDataObserver);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                if(newState==RecyclerView.SCROLL_STATE_IDLE){
                    autoPlay();
                }
            }

            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                if(playerTarget!=null&&playerTarget.isPlay()&&!isTargetInBounds(playerTarget)){
                    playerTarget.inActive();
                }
            }
        });
    }

    private RecyclerView.AdapterDataObserver mDataObserver =new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            autoPlay();
        }
    };

    private void autoPlay() {
        if(mTargets.size()<=0||recyclerView.getChildCount()<=0){
            return;
        }
        if(playerTarget!=null&&playerTarget.isPlay()&&isTargetInBounds(playerTarget)){
            return;
        }

        IPlayTarget playTarget=null;
        for (IPlayTarget mTarget : mTargets) {
            boolean targetInBounds = isTargetInBounds(mTarget);
            if(targetInBounds){
                playTarget=mTarget;
                break;
            }
        }
        if(playTarget!=null){
            if(playerTarget!=null&&playerTarget.isPlay()){
                playerTarget.inActive();
            }
            playerTarget=playTarget;
            playerTarget.onActive();
        }
    }

    private boolean isTargetInBounds(IPlayTarget mTarget) {
        ViewGroup owner = mTarget.getOwner();
        ensureRecycleViewLoaction();
        if(!owner.isShown()){
            return false;
        }
        int[] location=new int[2];
        owner.getLocationOnScreen(location);
        int center=location[1]+owner.getHeight()/2;
        return center>=rvLocation.x&&center<=rvLocation.y;
    }
    private Point rvLocation=null;
    private Point ensureRecycleViewLoaction() {
        if(rvLocation==null){
            int[] location = new int[2];
            recyclerView.getLocationOnScreen(location);
            int top=location[1];
            int bottom=top+recyclerView.getHeight();
            rvLocation = new Point(top, bottom);
        }
        return rvLocation;
    }

    public void onPause() {
        if(playerTarget!=null){
            playerTarget.inActive();
        }
    }

    public void onResume() {
        if(playerTarget!=null){
            playerTarget.onActive();
        }
    }
}
